<?php

class SQLQuery {
    protected $_dbHandle;
    protected $_result;

    /** Connects to database **/

    function connect($address, $account, $pwd, $name) {
        $this->_dbHandle = mysqli_connect($address, $account, $pwd, $name);
        
		if ($this->_dbHandle->connect_error) {
			die('Connect Error (' . $mysqli->connect_errno . ') '
					. $mysqli->connect_error);
		}
    }

    /** Disconnects from database **/

    function disconnect() {
        $this->_dbHandle->close(); 
    }

    function selectAll() {
    	$query = 'select * from `'.$this->_table.'`';
    	return $this->query($query);
    }

    function select($id) {
    	$query = 'select * from `'.$this->_table.'` where `id` = \''.mysql_real_escape_string($id).'\'';
    	return $this->query($query, 1);
    }

    /** Custom SQL Query **/
	function query($query, $singleResult = 0) {
		if(!$this->_dbHandle){
			throw new Exception("There is no connection to the database");
		}
		$this->_result = mysqli_query($this->_dbHandle, $query);
		
		if($this->_result){
			if (preg_match("/select/i",$query)) {
				$result = array();
				$table = array();
				$field = array();
				$tempResults = array();
				$numOfFields = mysqli_num_fields($this->_result);
				for ($i = 0; $i < $numOfFields; ++$i) {
					array_push($table,mysqli_fetch_field_direct($this->_result, $i)->table); // field table
					array_push($field,mysqli_fetch_field_direct($this->_result, $i)->name); // field name
				}

				while ($row = mysqli_fetch_row($this->_result)) {
					for ($i = 0;$i < $numOfFields; ++$i) {
						$table[$i] = trim(ucfirst($table[$i]),"s");
						$tempResults[$table[$i]][$field[$i]] = $row[$i];
					}
					if ($singleResult == 1) {
						mysqli_free_result($this->_result);
						return $tempResults;
					}
					array_push($result,$tempResults);
				}

				mysqli_free_result($this->_result);
				return($result);
			}
		}
	}
	
	function prepareStatement($statement){
		$stmt = mysqli_prepare( $this->_dbHandle , $statement);
		if ( false===$stmt ) {
            printf('prepare failed: %s', htmlspecialchars($this->_dbHandle->error));
            die;
        }
		return $stmt;
	}
	
	function executeStatement($statement, $params){
		$statement->execute();
		//$statement->bind_result($package_unit_id, $package_unit);
		call_user_func_array('mysqli_stmt_bind_param', array_merge (array($statement), $params)); 
		
		$resultSet = array();
		/* fetch values */
		while ($statement->fetch()) {
			array_push($resultSet, $params);
		}
		return $resultSet;
	}

    /** Get number of rows **/
    function getNumRows() {
        return mysql_num_rows($this->_result);
    }

    /** Free resources allocated by a query **/

    function freeResult() {
        mysql_free_result($this->_result);
    }

    /** Get error string **/

    function getError() {
        return mysql_error($this->_dbHandle);
    }
}











/*
if($conn->connect_error) {
	printf('connect error (%d) %s', $conn->connect_errno, htmlspecialchars($conn->connect_error));
				die;
}
$stmt = $conn->prepare('SELECT add_profile_images.image_id, add_profile_images.image_name FROM add_profile_images, users_profile_images WHERE users_profile_images.account_id=? AND users_profile_images.status=?');
if ( false===$stmt ) {
	printf('prepare failed: %s', htmlspecialchars($conn->error));
	die;
}

$rc = $stmt->bind_param('is',$account_id,$status);
if ( false===$rc ) {
	printf('bind_param failed: %s', htmlspecialchars($stmt->error));
die;
}

$rc= $stmt->execute();
if ( false===$rc ) {
		printf('execute failed: %s', htmlspecialchars($stmt->error));
		die;
}
$rc = $stmt->bind_result($id,$imageName);
if ( false===$rc ) {
		printf('bind_result failed: %s', htmlspecialchars($stmt->error));
		die;
}
*/